package com.anan.controller;

import com.anan.constants.Constants;
import com.anan.exception.BusinessException;
import com.anan.pojo.dto.StudentDTO;
import com.anan.pojo.dto.StudentIdDTO;
import com.anan.pojo.vo.StudentAndOpusVO;
import com.anan.pojo.vo.StudentAndProdInfoVO;
import com.anan.pojo.vo.StudentVO;
import com.anan.service.StudentService;
import com.anan.utils.ComResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author anan
 * @date 2023/03/15/11:28
 **/
@Slf4j
@RestController
@RequestMapping("/salvage/students")
public class StudentController {
    @Resource
    private StudentService studentService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ComResult register(@RequestBody StudentDTO studentDTO){
        ComResult comResult = new ComResult();
        if(studentDTO.getUsername() == null) {
            comResult.setMessage("用户名不能为空！");
        }
        if (studentDTO.getPassword() == null){
            comResult.setMessage("密码不能为空！");
        }

        try{
            int num = studentService.insertStudent(studentDTO);
            if (num > 0) {
                comResult.setCode(Constants.RET_CODE.SUCCESS);
                comResult.setMessage("注册成功！");
            } else {
                comResult.setCode(Constants.RET_CODE.FAILED);
                comResult.setMessage("注册失败！");
            }
        } catch (BusinessException e) {
            comResult.setCode(e.getCode());
            comResult.setMessage(e.getMessage());
        }
        return comResult;
    }

    @PostMapping ("/login")
    public ComResult login(@RequestBody StudentDTO studentDTO) {
        ComResult comResult = new ComResult();
        if (studentDTO != null) {
            if(studentDTO.getUsername() != null && studentDTO.getPassword() != null) {
                StudentVO studentVO = studentService.login(studentDTO);
                if(studentVO != null) {
                    comResult.setData(studentVO);
                    comResult.setCode(Constants.RET_CODE.SUCCESS);
                    comResult.setMessage("登录成功！");
                } else {
                    comResult.setCode(Constants.RET_CODE.FAILED);
                    comResult.setMessage("用户名或密码错误！");
                }
            }else {
                comResult.setCode(Constants.RET_CODE.FAILED);
                comResult.setMessage("用户名或密码不能为空！");
            }
        }
        return comResult;
    }

    @PostMapping("/updateStudentInfo")
    public ComResult updateStudentInfo(@RequestBody StudentDTO studentDTO){
        ComResult comResult = new ComResult();
        if (null != studentDTO) {
            if(StringUtils.isBlank(studentDTO.getUsername())){
                log.info("=====未传入需要修改的用户！=====");
                return comResult;
            }

            if (StringUtils.isBlank(studentDTO.getNickname()) && StringUtils.isBlank(studentDTO.getSex()) &&
                    StringUtils.isBlank(studentDTO.getPhoneNum()) && StringUtils.isBlank(studentDTO.getName())) {
                comResult.setCode(Constants.RET_CODE.FAILED);
                comResult.setMessage("请输入需要修改的信息");
            }

            boolean flag = studentService.updateStudentInfo(studentDTO);
            if (flag) {
                comResult.setCode(Constants.RET_CODE.SUCCESS);
                comResult.setMessage("修改成功！");
            } else {
                comResult.setCode(Constants.RET_CODE.FAILED);
                comResult.setMessage("修改失败！");
            }
        }
        return comResult;
    }

    /**
     * 获取用户所有商品信息
     */
    @PostMapping("/getStudentAndProdById")
    public ComResult getStudentAndProdById(@RequestBody StudentIdDTO studentIdDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if(null == studentIdDTO.getStudentId() || studentIdDTO.getStudentId() < 0) {
            comResult.setMessage("未选择用户或用户不存在！");
            return comResult;
        }

        StudentAndProdInfoVO studentAndProdInfoVO = studentService.getUserStudentAndProdByIdById(studentIdDTO.getStudentId());
        if(null != studentAndProdInfoVO) {
            comResult.setData(studentAndProdInfoVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("查询成功！");
        } else {
            comResult.setMessage("查询失败");
        }

        return comResult;
    }

    /**
     * 获取用户所有帖子
     */
    @PostMapping("/getStudentAndOpusById")
    public ComResult getStudentAndOpusById(@RequestBody StudentIdDTO studentIdDTO) {
        ComResult comResult = new ComResult();
        comResult.setCode(Constants.RET_CODE.FAILED);

        if(null == studentIdDTO.getStudentId() || studentIdDTO.getStudentId() < 0) {
            comResult.setMessage("未选择用户或用户不存在！");
            return comResult;
        }

        StudentAndOpusVO studentAndOpusVO = studentService.getStudentAndOpusById(studentIdDTO.getStudentId());
        if(null != studentAndOpusVO) {
            comResult.setData(studentAndOpusVO);
            comResult.setCode(Constants.RET_CODE.SUCCESS);
            comResult.setMessage("查询成功！");
        } else {
            comResult.setMessage("查询失败");
        }

        return comResult;
    }

}
